﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class BowlingBall : Actor
{
    //public Transform m_Target;
    //public float m_Speed = 5f;
    //public float m_MaxSpeed = 20f;

    private bool m_MouseControl = false;
    private Vector3 m_InitialPosition;
    private Vector3 m_DeltaPositionCamera;
    private Vector3 m_TargetDirection;

	protected override void Awake()
	{
		base.Awake ();
        m_DeltaPositionCamera = aTransform.position - Camera.main.transform.position;
        m_InitialPosition = aTransform.position;
        aRigidbody.isKinematic = true;
	}
	
	private void Update () 
    {
        /*
        if(m_MouseControl)
        {
            float distance = m_InitialPosition.z - Camera.main.transform.position.z;
            Debug.Log(m_InitialPosition.y);
            //m_TargetPos = m_InitialPosition;
            m_TargetPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
            m_TargetPos = Camera.main.ScreenToWorldPoint(m_TargetPos);
            m_TargetPos.y = m_InitialPosition.y;
            m_TargetPos.z = m_InitialPosition.z;
            
            aTransform.position = m_TargetPos;
        }
        */

        //Debug.DrawRay(aTransform.position, aTransform.forward * 10f, Color.red);
	}
    
    private void FixedUpdate () 
    {
        // FOLLOW TARGET
        //m_TargetDirection = (m_Target.position - aTransform.position).normalized;
        //Debug.DrawRay(aTransform.position, m_TargetDirection * 10f, Color.magenta);
        //aRigidbody.AddForce(m_TargetDirection * m_Speed);
        
        //if(aRigidbody.velocity.magnitude > m_MaxSpeed)
        //{
        //    aRigidbody.velocity = aRigidbody.velocity.normalized * m_MaxSpeed;
        //}
        
        //Debug.Log(aRigidbody.velocity.magnitude);

        /*
        if(Input.GetKey(KeyCode.UpArrow))
        {
            aRigidbody.AddForce(Vector3.forward * 1500f);
        }
        
        if(Input.GetKey(KeyCode.LeftArrow))
        {
            //aRigidbody.AddTorque(Vector3.up * -150f);
            aRigidbody.AddForceAtPosition(Vector3.left * 1500f, aTransform.position);
        }
        if(Input.GetKey(KeyCode.RightArrow))
        {
            //aRigidbody.AddTorque(Vector3.up * 1500f);
            aRigidbody.AddForceAtPosition(Vector3.right * 5000f, aTransform.position);
        }
        */
    }
    
    private void OnTouchTap()
    {
        //Debug.Log("TAP");
    }
    
    private void OnTouchEnter()
    {
        //Debug.Log("ENTER");
        m_MouseControl = true;
    }

    private void OnTouchOver()
    {
        //Debug.Log("OVER");
    }

	private void OnTouchStay()
    {
       //Debug.Log("STAY");
	}
    
    private void OnTouchExit()
    {
        //Debug.Log("EXIT");
    }
    
    private void OnTouchReleased()
    {
        //Debug.Log("RELEASED");
        m_MouseControl = false;
    }
    
    private void OnTouchUp()
    {
        //Debug.Log("UP");
    }

    public void ThrowBall()
    {
        aRigidbody.isKinematic = false;
        //Vector3 direction = (GameplayManager.Instance.LineDirections[1] - GameplayManager.Instance.LineDirections[0]).normalized;
        Vector3 direction = (GameplayManager.Instance.LineDirections[GameplayManager.Instance.LineDirections.Count - 1] - GameplayManager.Instance.LineDirections[0]).normalized;
        direction.y = 0;
        
        Debug.Log("BEFORE CLAMP = " + direction.x.ToString());
        if(Mathf.Abs(direction.x) < GameplayManager.Instance.m_StraightLineThreshold)
        {
            direction.x = Mathf.Clamp(direction.x, -GameplayManager.Instance.m_MinStraightLineDirection, GameplayManager.Instance.m_MinStraightLineDirection);
        }    
        else
        {
            direction.x = Mathf.Clamp(direction.x, -GameplayManager.Instance.m_DirectionMaxLimit, GameplayManager.Instance.m_DirectionMaxLimit);
        }

        Debug.Log("AFTER CLAMP = " + direction.x.ToString());
        //direction.z *= -1;
        aRigidbody.AddForce(direction * GameplayManager.Instance.m_ThrowSpeed, ForceMode.Impulse);
    }

}
